package Utils;

import model.User;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

/**
 * add.do     添加
 * editUser.do 修改用户
 * edit.do    修改
 * delete.do  删除
 * deleteUser.do  删除
 * find.do    查询
 */
public class CoreServlet extends HttpServlet {
    protected void service(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String path = request.getRequestURI();// path = /lyb_war_exploded4/add.do
        String flag = path.substring(path.lastIndexOf('/') + 1, path.lastIndexOf('.')); // flag = add
        debug(flag);
        request.setCharacterEncoding("utf-8");
        DBUtils dob = null;
        Object user = request.getSession().getAttribute("user");
        User user1 = (User) user;
        try {
            dob = new DBUtils();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }

        if ("editUser".equals(flag)) {//修改留言

            String id = request.getParameter("id");
            String name = request.getParameter("name");
            String password = request.getParameter("password");
            String[] field = {"name", "password"};
            String[] value = {name, password};
            dob.modifyData("user", field, value, 0,"id=" + id);
            debug("修改用户信息");
            //请求分派
            if (user1.getLevel() == 1) {
                gotoPage(request, response, "/admin.jsp");
            }else if (user1.getLevel() == 2){
                gotoPage(request, response, "/user.jsp");
            }
        }

        if ("add".equals(flag)) {//添加留言

            String title = request.getParameter("title");
            String content = request.getParameter("content");
            int share = Integer.parseInt(request.getParameter("share"));
            System.out.println("SERVLET SHARE:  "+share);


            String userid = String.valueOf(user1.getId());

            //取得当前时间并格式化
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINESE);
            Date date = new Date();
            String dateStr = sdf.format(date);
            //插入数据
            String[] fields = {"userid", "title", "content", "time"};
            String[] values = {userid, title, content, dateStr};

            dob.insertData("classnote", fields, values, share);
            debug("添加笔记");
            //请求分派
            if (user1.getLevel() == 1) {
                gotoPage(request, response, "/admin.jsp");
            }else if (user1.getLevel() == 2){
                gotoPage(request, response, "/user.jsp");
            }

        }
        if ("edit".equals(flag)) {//修改留言

            String id = request.getParameter("id");
            String title = request.getParameter("title");
            String content = request.getParameter("content");
            int share = Integer.parseInt(request.getParameter("share"));
            String[] field = {"title", "content"};
            String[] value = {title, content};
            dob.modifyData("classnote", field, value, share,"id=" + id);
            debug("修改笔记");
            //请求分派
            if (user1.getLevel() == 1) {
                gotoPage(request, response, "/admin.jsp");
            }else if (user1.getLevel() == 2){
                gotoPage(request, response, "/user.jsp");
            }
        }
        if ("delete".equals(flag)) {//删除留言
            debug("删除留言");
            String id = request.getParameter("id");
            dob.deleteData("classnote", "id=" + id);
            //请求分派
            if (user1.getLevel() == 1) {
                gotoPage(request, response, "/admin.jsp");
            }else if (user1.getLevel() == 2){
                gotoPage(request, response, "/user.jsp");
            }
        }

        if ("deleteUser".equals(flag)) {//删除留言
            debug("删除用户");
            String id = request.getParameter("id");
            dob.deleteData("user", "id=" + id);
            //请求分派
            if (user1.getLevel() == 1) {
                gotoPage(request, response, "/admin.jsp");
            }else if (user1.getLevel() == 2){
                gotoPage(request, response, "/user.jsp");
            }
        }


    }

    private void gotoPage(HttpServletRequest request, HttpServletResponse response, String path)
            throws ServletException, IOException {
        //请求分派
        RequestDispatcher rd = request.getRequestDispatcher(path);
        rd.forward(request, response);
    }

    public static void debug(String str) {
        System.out.println(str);
    }
}
